<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Home\Controller;

use Think\Controller;

/**
 * 林州市悟能科技提供技术支持
 * 400-037203
 * @author Mr_Lee :672288511;
 */
class UserController extends Controller
{
    public function _initialize()
    {
        if (empty($_SESSION['username'])) {

            $this->error('页面加载中。。。', U('Login/Login'), 1);

        }
        $table1="user";
        $this->assign('table',$table1);
    }

    public function Userinfo()
    {
        if (IS_GET) {
          $map['name']=array('like','%'.I('get.keyword').'%');
          $map['tel']=array('like','%'.I('get.keyword').'%');
          $map['_logic']='or';
        }
        $wxuser = M('wxuser');
        $count = $wxuser->where($map)->count();
        $Page = new \Think\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $show = $Page->show();// 分页显示输出

        $us = $wxuser->where($map)->order('uid DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('page', $show);
        $this->assign('us', $us);
        $this->display();

    }



    public function sijiinfo()
    {
        if(IS_POST){
            $keyword = I('keyword');
            $where['xingming|dianhua'] = array('like','%'.$keyword.'%');
            // dump($where);
        }
        
        // $member_list = M('siji as a')->where($where1)->join('hy_wxuser as b on a.openid=b.openid')->select();
        // dump($member_list);die;
        $count = M('siji')->where($where)->count();
        $Page = new \Think\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $show = $Page->show();// 分页显示输出
        $siji = M('siji')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order('id DESC')->select();
        $this->assign('siji', $siji);
        $this->assign('page', $show);
        $this->display();


    }

    public function Userxg()
    {
        $User = M('Wxuser');
        if (IS_AJAX) {
            $data['uid'] = I('post.uid');
            $data['nickname'] = I('post.nickname');
            $data['name'] = I('post.name');
            $data['tel'] = I('post.tel');
            $data['yue'] = I('post.yue');

            $updata = $User->save($data); // 根据条件保存修改的数据
            if ($updata) {
                if (!empty($_POST['yue'])) {
                    $da['title'] = "用户余额变更";
                    $da['info'] = 'uid:' . I('post.uid') . '余额变更为' . I('post.yue');
                    $da['addtime'] = time();
                    $da['adduser'] = session('username');
                    $add = M('adminlog')->add($da);

                }
                $data = 0;
            } else {

                $data = 1;
            }
            echo $data;

        } else {

            $map['uid'] = I('get.id');
            $us = $User->where($map)->find();
            $this->assign('us', $us);
            $this->display();

        }


    }


    /**
     * @func审核司机，查看司机的信息是否完整，把审核的接口写入消息表中，通知司机端
     *
     */
    public function sijicheck(){
        if (IS_AJAX) {
            $data = I('post.');
            if(empty($data['openid'])){
                datamsg(0,'系统错误');
            }
            M()->startTrans();
            $mess['send_id']=0;
            $mess['recevice_openid']=$data['openid'];
            $mess['ctime']=time();
            $mess['content']=$data['description'];
            $mess_result = M('message')->add($mess);
            if($mess_result){
                $sijicheck = M('siji')->where(array('openid'=>$data['openid']))->save(array('status'=>$data['status']));
                if($sijicheck){
                    M()->commit();
                    datamsg(1,'审核成功');
                }else{
                    M()->rollback();
                    datamsg(0,'审核失败，系统出错');
                }
            }else{
                datamsg(0,'写入消息表的时候失败了');
            }

        }else{
            $openid = I('get.openid');
            $siji = M('siji')->where(array('openid'=>$openid))->find();
            $this->assign('siji',$siji);
            $this->display();
        }
    }



    public function addsiji()
    {
        if (IS_POST) {
            $data=I("post.");
            if(empty($data['dianhua'])){
                datamsg(0,'电话号码必须填写');
            }
            $data['create_jztime']=strtotime($data['create_jztime']);
            $data['status']=0;
            $data['openid']='wap_'.$data['dianhua'];

            $wxuser['openid'] = 'wap_'.$data['dianhua'];
            if($wxuser['openid']){
                $wxuserarr = M('wxuser')->where(array('openid'=>$wxuser['openid']))->find();
                if($wxuserarr){
                    datamsg(0,'对不起，该号码已经存在');
                }
            }
            //$wxuser['tel']=$data['dianhua'];
            $wxuser['last_login_time']=time();
            M()->startTrans();
            $add = M('siji')->add($data);
            if ($add) {
                $user=M('wxuser')->add($wxuser);
                if($user){
                    M()->commit();
                    datamsg(1,'插入成功');
                }else{
                    M()->rollback();
                    datamsg(0,'用户信息写入失败');
                }
            } else {
                datamsg(0,'司机信息写入失败');
            }
        } else {
            $this->display();
        }
    }



    public function savesiji()
    {
        if (IS_POST) {
            $data=I("post.");
            $data['create_jztime']=strtotime($data['create_jztime']);
            $data['status']=0;
            $wxuser['tel']=$data['dianhua'];
            $wxuser['last_login_time']=time();
            M()->startTrans();
            $add = M('siji')->where(array('openid'=>$data['openid']))->save($data);
            if ($add) {
                $user=M('wxuser')->where(array('openid'=>$data['openid']))->save($wxuser);
                if($user){
                    M()->commit();
                    datamsg(1,'保存成功');
                }else{
                    M()->rollback();
                    datamsg(0,'用户信息写入失败');
                }
            } else {
                datamsg(0,'司机信息写入失败');
            }
        } else {
            $this->display();
        }
    }



    public function sijixg()
    {
        $User = M('siji');
        if (IS_AJAX) {
            $data = I('post.');
            $siji = $User->where(array('openid'=>$data['openid']))->find();

            switch($data['oper']) {
                case "car":
                    $data['certifydatea']=strtotime($data['certifydatea']);
                    $data['insureff']=strtotime($data['insureff']);
                    $data['insurexp']=strtotime($data['insurexp']);
                    $data['nextFixdate']=strtotime($data['nextFixdate']);
                    $data['ownername']=$siji['xingming'];
                    $data['fixstate']=$data['fixstate'];
                    $data['flag']=2;
                    $data['updatetime']=time();
                    $result = M('car_basic')->where(array('openid' => $data['openid']))->save($data);
                    M('siji')->where(array('openid'=>$data['openid']))->save(array('status'=>0));
                    break;
                case "driver":
                    $data['DriverBirthday']=strtotime($data['DriverBirthday']);
                    $data['GetDriverLicenseDate']=strtotime($data['GetDriverLicenseDate']);
                    $data['DriverLicenseOn']=strtotime($data['DriverLicenseOn']);
                    $data['DriverLicenseOff']=strtotime($data['DriverLicenseOff']);
                    $data['GetDriverLicenseDatew']=strtotime($data['GetDriverLicenseDatew']);
                    $data['NetworkCarlssueDate']=strtotime($data['NetworkCarlssueDate']);
                    $data['DriverLicenseOffw']=strtotime($data['DriverLicenseOffw']);
                    $data['CourseDate']=strtotime($data['CourseDate']);
                    $data['StartDate']=strtotime($data['StartDate']);
                    $data['StopDate']=strtotime($data['StopDate']);
                    $data['StartTime']=strtotime($data['StartTime']);
                    $data['StopTime']=strtotime($data['StopTime']);
                    $data['UpdateTime']=time();
                    $result = M('basic_driver_information')->where(array('openid' => $data['openid']))->save($data);
                    M('siji')->where(array('openid'=>$data['openid']))->save(array('status'=>0));
                    break;
                default:
                    datamsg(0,'操作参数不正确');
            }
            if ($result) {
                datamsg(1, '修改司机的信息成功');
            } else {
                datamsg(0, '修改司机的信息失败');
            }



        } else {
            $map['openid'] = I('get.openid');
            $us = $User->where($map)->find();//获取司机的基本信息
            $car = M('car_basic')->where($map)->find();//获取小车的基本信息

            $basicdriver = M('basic_driver_information')->where($map)->find();//获取小车的基本信息
            $this->assign('us', $us);
            $this->assign('car',$car);
            $this->assign('basicdriver',$basicdriver);
            $this->display();
        }
    }





    public function delsj()
    {

        $map['id'] = I('get.id');

        $User = M('siji');

        $del = $User->where($map)->delete();
        if ($del) {

            $this->success('删除成功');

        } else {

            $this->error('删除失败');

        }
    }

    public function del()
    {

        $map['uid'] = I('get.id');

        $User = M('Wxuser');

        $del = $User->where($map)->delete();
        if ($del) {

            $this->success('删除成功');

        } else {

            $this->error('删除失败');

        }


    }


    public function fankui_list()
    {
       $count=M('feedback')->count();
        $page = new \Think\Page($count,10);
        $p = $_GET['p'];
        $show = $page->show();
        
        $list = M('feedback')->select();

        if(is_array($list))
        {   
            foreach ($list as $k=>$v) {
                $list[$k][]=D('wxuser')->where('uid='.$v['user_id'])->find();
            }
        }

        
        $this->assign('list', $list);
        $this->display();
    }


    //显示司机的详细信息
    public function sijishow(){
        $openid         = I('get.openid');
        $map['openid']  = $openid;
        $wxuser = M('wxuser')->where(array('openid'=>$openid))->find();     //获取注册的基本信息
        $siji = M('siji')->where(array('openid'=>$openid))->find();         //司机信息
        $car = M('car_basic')->where(array('openid'=>$openid))->find();         //司机车辆信息
        $infos  = M('basic_driver_information')->where(array('openid' => $openid))->find();
        $this->assign('infos',$infos);
        !empty($wxuser['last_login_time'])?'':$wxuser['last_login_time']='';


        $member = M('wxuser')->where($map)->find();//获取司机用户的基本信息
        $endToday   = getWeek2($member);
        
        $this->assign(array(
            'order_price'   => $endToday['zongshouru']?'':0,
            'total'         => $endToday['ketixian']?'':0,
            'member'        => $member,
            ));


            if (I('post.wtime')) {
                $wtime = strtotime(I('post.wtime'));
            } else {
                $wtime = 0;
            }
            if (I('post.ntime')) {
                $ntime = strtotime(I('post.ntime'));
            } else {

                $ntime = time();
            }

            $this->assign('wtime', I('post.wtime'));
            $this->assign('ntime', I('post.ntime'));
            $where1 = array(
                'openid'  => $map['openid'],
                'addtime' => array('between', '' . $wtime . ',' . $ntime . ''),
                'xingming' => array('like', '%' . I('post.sj').'%'),
            );

            $where2 = array(
            // 'pay_statu' => 1,
            '_complex' => $where1, // 复合查询

            );
            $where3 = array(
                'status' => 6,
                // 'order_statu'=>1,
                '_complex' => $where1, // 复合查询

            );
            $where4 = array(
                'status' => 6,
                // 'order_statu'=>1,
                '_complex' => $where1, // 复合查询

            );
            // dump($where2);
            $order = M('xllog');
            $bwcount = $order->where($where2)->count('id');
            $bwcount2 = $order->where($where2)->sum('price');
            $bwcount3 = $order->where($where3)->count('id');
            $bwcount4 = $order->where($where4)->sum('price');
            $this->assign('bwcount', $bwcount);
            $this->assign('bwcount2', $bwcount2);
            $this->assign('bwcount3', $bwcount3);
            $this->assign('bwcount4', $bwcount4);


        $this->assign('user',$wxuser);//个人注册信息
        $this->assign('siji',$siji);//司机个人信息
        $this->assign('car',$car);//司机车辆个人信息
        $this->display();
    }


    //图片上传类
    public function uploadpic(){
        $currentdate='./Uploads/sign/';
        if(!is_dir($currentdate)){
            mkdir ($currentdate,0777,true);
        }
        $config = array(
            'maxSize'    =>    3145728,
            'rootPath'   =>    $currentdate,
            'savePath'   =>    '',
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'saveName' => array('uniqid',''),
            'autoSub'    =>    false,
        );
        $upload = new \Think\Upload($config);
        $info = $upload->upload();
        if(!$info){
            datamsg(0,"没有上传成功");
        }else{
            $srcimg=array();
            $info_key=array_keys($info);
            foreach($info_key as $k=>$v){
                $srcimg[$k]=substr($currentdate.$info[$v]['savename'],1);
            }
            datamsg(1,"上传成功",$srcimg);
        }
    }

    /**
     * Notes:车辆信息
     * @return [type] [description]
     */
    public function cheliang()
    {
        if(IS_POST){
            $keyword = I('keyword');
            $where['xingming|dianhua'] = array('like','%'.$keyword.'%');
        }
        $count = M('siji')->where($where)->count();
        $Page = new \Think\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $show = $Page->show();// 分页显示输出
        $siji = M('siji')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order('id DESC')->select();
        $this->assign('siji', $siji);
        $this->assign('page', $show);
        $this->display();
    }
}
